home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / aecdev.aedaemon / aedaemonaevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.5 KB  |  129 lines

  1. /*
  2.     File:        AEDaemonAEvents.c
  3.  
  4.     Contains:    Here are the AppleEvent handlers for this background-only task.    
  5.                 Since this is a real live application, it can accept and send any    
  6.                 Apple Events you want to send or recieve.    
  7.                 In this case, the only ones that make any sense in this app are    
  8.                 'oapp' and 'quit'
  9.  
  10.     Written by:     
  11.  
  12.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */ 
  27.  
  28. #include "AEDaemon.h"
  29. extern Boolean gQuit;
  30. extern EventRecord ERecord;
  31. extern Boolean gHasAppleEvents;
  32.  
  33. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  34. /* installs our event handlers. */
  35. /* if the AEM isn't around, we bail. */
  36. void InitAEStuff(void)
  37. {
  38.  
  39.     OSErr aevtErr = noErr;
  40.     long aLong = 0;
  41.     Boolean gHasAppleEvents = false;
  42.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  43.     *   then we exit */
  44.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  45.     /* The following series of calls installs all our AppleEvent Handlers.
  46.     *   These handlers are added to the application event handler list that 
  47.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  48.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  49.     *   list of handlers and dispatch to it if there is one.
  50.     */
  51.     if (gHasAppleEvents) {
  52.  
  53.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, 
  54.              NewAEEventHandlerProc(AEOpenHandler),0, false);
  55.              if (aevtErr)  ExitToShell();
  56.  
  57.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, 
  58.              NewAEEventHandlerProc(AEOpenDocHandler),0, false);
  59.              if (aevtErr)  ExitToShell();
  60.  
  61.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, 
  62.              NewAEEventHandlerProc(AEQuitHandler), 0, false);
  63.              if (aevtErr)  ExitToShell();
  64.  
  65.          aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, 
  66.              NewAEEventHandlerProc(AEPrintHandler),0, false);
  67.              if (aevtErr)  ExitToShell();
  68.     } else {
  69.         ExitToShell();
  70.     }
  71. }
  72.  
  73. /* end InitAEStuff */
  74.  
  75. void DoHighLevel(EventRecord *AERecord)
  76. {
  77.     /* I'm not doing any error handling here because there's not a lot */
  78.     /* I can do, just pass the errors back. */
  79.     AEProcessAppleEvent(AERecord);
  80.     
  81. }
  82.  
  83. /* end DoHighLevel */
  84.  
  85.  
  86. /* This is the standard Open Application event.   */
  87. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  88. {
  89. #pragma unused (messagein,reply,refIn)
  90.     /* we of course don't do anything here, since we're background only */
  91.     return(noErr);
  92. }
  93.  
  94. /* end AEOpenHandler */
  95.  
  96. /* Open Doc, opens our documents. */
  97. /* In this case, of course, you are in the background so you should return an error */
  98. /* here since you're not opening a document. */
  99. /* Of course, you _might_ want to open a doc, but you will probably */
  100. /* confuse the user if you do, since they will see no action as the */
  101. /*  result of their clicking on a document icon. */
  102. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  103. {
  104. #pragma unused (reply, refIn,messagein)
  105.     /* we of course don't do anything here, so tell the sender that we  */
  106.     /* didn't handle the event */
  107.     return(errAEEventNotHandled);
  108. }
  109.  
  110. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  111. {
  112. #pragma unused (reply,refIn,messagein)
  113.     /* we of course don't do anything here */
  114.     return(errAEEventNotHandled);
  115. }
  116.  
  117. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  118. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  119. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  120. {
  121. #pragma unused (messagein,refIn,reply)
  122.     /*  This does _NOT_ quit, you */
  123.     /* should NEVER quit from an AppleEvent handler.  Calling */
  124.     /* ExitToShell here would blow things up */
  125.     gQuit = true;
  126.     return(noErr);
  127. }
  128.  
  129.